/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.risk.service.api.CorpManagementService;
import com.koron.scene.domain.ProjectTraining;
import com.koron.scene.service.api.ProjectTrainingService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "ProjectTrain", description = "项目相关方-相关方培训")
@RestController
@RequestMapping("/projectTrain")
public class ProjectTrainingController {

    private final static Logger logger= LoggerFactory.getLogger(ProjectTrainingController.class);
    @Autowired
    private ProjectTrainingService projectTrainingService;

    @Autowired
    CorpManagementService corpService;

    @ApiOperation("相关方培训导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "file", value = "上传的excel文件", required = true, paramType = "query", dataType = "file"),
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
    })
    @RequestMapping(value = "/importExcel.htm", method = {RequestMethod.POST})
    @Log(title = "相关方培训导入", businessModule = "项目培训", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> importFromExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                          @RequestBody MultipartFile file, String projectId, String enterpriseId,
                                          String corpCode) {


        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectTrainingService.importExcel(user, file, projectId, enterpriseId);
    }


    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    @ApiOperation(value = "获取教育培训信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffName", value = "人员名称", dataType = "String"),
            @ApiImplicitParam(name = "trainName", value = "课程名称", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                               String staffName, String trainName, String projectId, String enterpriseId,
                               Integer page, Integer pageSize) {

        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectTrainingService.list(user, staffName, trainName, projectId, enterpriseId, page, pageSize);
    }

    @RequestMapping(value = "/saveUpdate.htm", method = RequestMethod.POST)
    @ApiOperation(value = "修改教育培训信息")
    @Log(title = "修改教育培训信息", businessModule = "项目培训", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> saveUpdate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                     @RequestBody List<ProjectTraining> projectTrainingList) {

        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectTrainingService.saveUpdate(user, projectTrainingList);
    }

    @ApiOperation(value = "删除教育培训信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectTrainingId", value = "人员持证id", dataType = "String"),
    })
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    @Log(title = "删除教育培训信息", businessModule = "项目培训", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 String projectTrainingId) {

        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectTrainingService.delete(projectTrainingId);
    }


    @ApiOperation("下载模板")
    @RequestMapping(value = "/exportExcelTemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportExcelTemplate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/ProjectTrainingTemplate.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/ProjectTrainingTemplate.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方人员教育培训导入模板", targetFile, "default.btl", map);
    }

    @SuppressWarnings("rawtypes")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffName", value = "人员名称", dataType = "String"),
            @ApiImplicitParam(name = "trainName", value = "课程名称", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String")
    })
    @ApiOperation("导出")
    @RequestMapping(value = "/exportList.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String staffName, String trainName, String projectId, String enterpriseId) {
        if (user == null) {
            return null;
        }
        MessageBean<?> msg = projectTrainingService.exportList(user, staffName, trainName, projectId, enterpriseId);
        if (msg.getData() == null) {
            return null;
        }
        File targetFile = new File("static/excel/ProjectTraining.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/ProjectTraining.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        List<ProjectTraining> data = (List<ProjectTraining>) msg.getData();
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        return Excel.createFile("相关教育培训", targetFile, "ProjectTraining.btl", map);
    }
}
